package com.tzbank.wisdom.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.Callable;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tzbank.esb.util.EsbConsumerUtil;
import com.tzbank.redis.RedisUtils;
import com.tzbank.utils.AmountUtil;
import com.tzbank.utils.CodeUtil;
import com.tzbank.utils.DateUtils;
import com.tzbank.utils.ResultCode;
import com.tzbank.utils.ResultUtil;
import com.tzbank.wisdom.constant.SysConstans;
import com.tzbank.wisdom.service.AccountManageService;
import com.tzbank.wisdom.service.CommonService;
import com.tzbank.wisdom.service.MyCardPackageService;
import com.tzbank.wisdom.utils.BusinessConstant;
import com.tzbank.wisdom.utils.RequestMap;
import com.tzbank.wisdom.utils.ThreadPoolFactory;
import com.tzbank.wisdom.utils.TransCodeEnum;

import jodd.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Slf4j
@Service(value = "accountManageService")
public class AccountManageServiceImpl implements AccountManageService {
	@Autowired
	private MyCardPackageService myCardPackageService;
	@Autowired
	private CommonService commonService;

	public Map<String, Object> updateAcctAlias(Map<String, String> reqBody) {
		log.info("请求参数【{}】", JSONObject.fromObject(reqBody).toString());

		// 组装入参Map
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> reqSysHead = RequestMap.getReqSysHead("11002000033", "04", "",
				BusinessConstant.SOURCRE_TYPE_PMBBS);
		Map<String, Object> userMap = RequestMap.getInputUserInfo((String) reqBody.get("clientSeqNo"));
		if (userMap.get("clientNo") == null) {
			return userMap;
		}
		reqSysHead.putAll(userMap);
		// 调用核心计价因子报文头入参
		inputMap.put("sysHead", reqSysHead);
		List s = new ArrayList();
		Map map = new HashMap();
		map.put("acctNo", reqBody.get("acctNo"));// 账号
		map.put("acctSerialNo", reqBody.get("acctSerialNo"));// 账户序号
		map.put("acctType", reqBody.get("acctType"));// 账户类型
		map.put("acctAlias", reqBody.get("acctAlias"));// 账户别名
		s.add(map);
		inputMap.put("acctArray", s);

		// inputMap.put("logonType", "R");//登录方式
		inputMap.put("sendBranchId", "");// 发送方机构标识码
		inputMap.put("clientIp", reqBody.get("ip"));// 客户端ip
		inputMap.put("ip", "");// IP地址
		inputMap.put("userId", userMap.get("userId"));// IP地址

		Map<String, Object> data = null;
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);

		log.info("响应【{}】", JSONObject.fromObject(result));
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
			return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
					result.get("data"));
		} else {
			data = (Map<String, Object>) result.get("data");
		}
		return ResultUtil.resultMap("成功", ResultCode.RESULT_CODE_SUCCESS.code(), data);
	}

	/**
	 * 下挂账户验证 个人网银可加挂列表查询
	 */
	public Map<String, Object> queryHangingAccount(Map<String, String> reqBody) {
		log.info("请求参数【{}】", JSONObject.fromObject(reqBody).toString());

		// 组装入参Map
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> reqSysHead = RequestMap.getReqSysHead("11003000017", "05", "",
				BusinessConstant.SOURCRE_TYPE_PMBBS);
		Map<String, Object> userMap = RequestMap.getInputUserInfo((String) reqBody.get("clientSeqNo"));
		if (userMap.get("clientNo") == null) {
			return userMap;
		}

		Map<?, ?> acclist = RedisUtils.getUserInfo(reqBody.get("clientSeqNo"));
		if (acclist != null) {
			List list = (List) acclist.get("acctArray");
			List newList = new ArrayList();
			for (int i = 0; i < list.size(); i++) {
				Map m = (Map) list.get(i);
				Map newMap = new HashMap();
				newMap.put("acctNo", m.get("acctNo"));// 账号
				newMap.put("acctSerialNo", m.get("acctSerialNo"));// 账户序号
				newMap.put("acctAlias", m.get("acctAlias"));// 账户别名
				newMap.put("branchType", m.get("acctType"));
				newMap.put("openOrgan", m.get("openBranch"));
				newList.add(newMap);
			}
			inputMap.put("globalId", acclist.get("globalId"));// 证件号码
			inputMap.put("globalType", acclist.get("globalType"));// 证件类型
			inputMap.put("listArray", newList);
			

		}
		inputMap.put("bussName", "TransJnlsQry");
		inputMap.put("userNo", userMap.get("eUserNo"));
		inputMap.put("eUserNo", userMap.get("eUserNo"));
		reqSysHead.putAll(userMap);
		// 调用核心计价因子报文头入参
		inputMap.put("sysHead", reqSysHead);
		// inputMap.put("logonType", "R");//登录方式
		inputMap.put("sendBranchId", "");// 发送方机构标识码inputMap.put("listArray", list);
		inputMap.put("clientIp", reqBody.get("ip"));// 客户端ip
		inputMap.put("ip", "");// IP地址
		inputMap.put("userId", userMap.get("userId"));// IP地址

		Map<String, Object> data = null;
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);

		log.info("响应【{}】", JSONObject.fromObject(result));
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
			return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
					result.get("data"));
		} else {

			data = (Map<String, Object>) result.get("data");
			data.remove("head");

		}
		return ResultUtil.resultMap("成功", ResultCode.RESULT_CODE_SUCCESS.code(), data);
	}

	@Override
	public Map<String, Object> getOperLogs(Map<String, String> paramMap) {
		log.info("入参:[{}]", JSONObject.fromObject(paramMap));
		Map<String, Object> inputMap = new HashMap<String, Object>();

		Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.OPER_LOG_QRY.getServiceCode(),
				TransCodeEnum.OPER_LOG_QRY.getServiceSence(), "0110", SysConstans.ABP_CODE, "");

		Map<String, Object> userMap = RequestMap.getInputUserInfo((String) paramMap.get("clientSeqNo"));
		if (userMap.get("clientNo") == null) {
			return userMap;
		}
		reqSysHead.putAll(userMap);
		inputMap.put("bussName", "TransJnlsQry");
		inputMap.put("userNo", userMap.get("eUserNo"));
		inputMap.put("eUserNo", userMap.get("eUserNo"));
		// 调用核心计价因子入参
		inputMap.put("sysHead", reqSysHead);
		String endDate = DateUtils.dateToDateString(new Date(), DateUtils.yyyy_MM_dd_EN);
		String startDate = "";
		// 传入类型，判断查询区间
		if ("01".equals(paramMap.get("dateInterval"))) {// 一周
			startDate = DateUtils.dateToDateString(DateUtils.addDays(new Date(), -7), DateUtils.yyyy_MM_dd_EN);
			inputMap.put("startTime", startDate);
			inputMap.put("endTime", endDate);
		} else if ("02".equals(paramMap.get("dateInterval"))) {// 一月内
			startDate = DateUtils.getDateOfMon(endDate, -1, DateUtils.yyyy_MM_dd_EN);
			inputMap.put("startTime", startDate);
			inputMap.put("endTime", endDate);
		} else if ("03".equals(paramMap.get("dateInterval"))) {// 三月内
			startDate = DateUtils.getDateOfMon(endDate, -3, DateUtils.yyyy_MM_dd_EN);
			inputMap.put("startTime", startDate);
			inputMap.put("endTime", endDate);
		} else {
			inputMap.put("startTime", paramMap.get("startDate"));
			inputMap.put("endTime", paramMap.get("endDate"));
		}
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
		log.info("esb 响应[{}]", JSONObject.fromObject(result));
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
			return ResultUtil.resultMap(result.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
					result.get("data"));
		}
		Map<String, Object> map = (Map<String, Object>) result.get("data");
		List listArray = (List) map.get("listArray");
		Map<String, Object> backMap = new HashMap<String, Object>();

		List backList = new ArrayList();
		for (int i = 0; i < listArray.size(); i++) {
			Map<String, Object> realDate = new HashMap<String, Object>();
			Map<String, Object> params = (Map<String, Object>) listArray.get(i);
			realDate.put("oriTranTime", (String) params.get("oriTranTime"));// 时间
			realDate.put("amt", params.get("amt") + "");// 金额
			realDate.put("productName", params.get("productName") + "");// 产品名称
			realDate.put("payerAcctNo", params.get("payerAcctNo") + "");//
			realDate.put("tranResult", params.get("tranResult") + "");// 交易结果
			realDate.put("statusType", params.get("statusType") + "");// 交易结果
			backList.add(realDate);
		}
		backMap.put("startDate", startDate);
		backMap.put("endDate", endDate);
		backMap.put("resultList", backList);
		return ResultUtil.resultMap(ResultCode.RESULT_CODE_SUCCESS.desc(), ResultCode.RESULT_CODE_SUCCESS.code(),
				backMap);
	}

	/**
	 * 交易明细查询 （单卡） /* inputMap.put("acctNo","6224271190391118");
	 * inputMap.put("acctSerialNo","101"); inputMap.put("startDate","20180930");
	 * inputMap.put("endDate","20181007");
	 */

	public Map<String, Object> queryAccountTransactionDetail(Map<String, Object> body) {
		Map<String, Object> reqSysHead = RequestMap.getReqSysHead(
				TransCodeEnum.ACCOUNT_TRANSACTION_DETAIL.getServiceCode(),
				TransCodeEnum.ACCOUNT_TRANSACTION_DETAIL.getServiceSence(), "", SysConstans.ABP_CODE, "");
		Map<String, Object> userMap = RequestMap.getInputUserInfo((String) body.get("clientSeqNo"));
		if (userMap.get("clientNo") == null) {
			return userMap;
		}

		reqSysHead.putAll(userMap);
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> AppHeadMap = new HashMap<String, Object>();
		// 调用核心计价因子报文头入参
		inputMap.put("sysHead", reqSysHead);
		AppHeadMap.put("currentNum", body.get("currentNum"));// 当前页码
		AppHeadMap.put("totalNum", body.get("totalNum"));// 本页记录总数
		inputMap.put("appHead", AppHeadMap);
		// 报文体
		inputMap.put("acctNo", body.get("acctNo"));
		inputMap.put("acctSerialNo", body.get("acctSerialNo"));
		// 排序 1-按日期升序 2-按日期倒序 不送则默认2-按日期倒序 SERIAL_FIELD_TYPE
		if ("1".equals(body.get("serialFieldType"))) {
			inputMap.put("serialFieldType", "1");
		} else {
			inputMap.put("serialFieldType", "2");
		}

		String dateInterval = "";
		String endDate = DateUtils.dateToDateString(new Date(), DateUtils.yyyyMMdd_EN);
		String startDate = "";
		// 传入类型，判断查询区间
		if ("01".equals(body.get("dateInterval"))) {// 一周
			startDate = DateUtils.dateToDateString(DateUtils.addDays(new Date(), -7), DateUtils.yyyyMMdd_EN);
			inputMap.put("startDate", "20180930");
			inputMap.put("endDate", "20181007");
			dateInterval = "一周内";
		} else if ("02".equals(body.get("dateInterval"))) {// 一月内
			startDate = DateUtils.getDateOfMon(endDate, -1, DateUtils.yyyyMMdd_EN);
			inputMap.put("startDate", "20180930");
			inputMap.put("endDate", "20181007");
			dateInterval = "本月";
		} else if ("03".equals(body.get("dateInterval"))) {// 三月内
			startDate = DateUtils.getDateOfMon(endDate, -3, DateUtils.yyyyMMdd_EN);
			inputMap.put("startDate", "20180930");
			inputMap.put("endDate", "20181007");
			dateInterval = "三月内";
		} else {
			inputMap.put("startDate", body.get("startDate"));
			inputMap.put("endDate", body.get("endDate"));
			dateInterval = "";
		}

		// 筛选条件
		inputMap.put("oppoAcctName", body.get("oppoAcctName"));// 对方账户名
		inputMap.put("drCrFlag", body.get("drCrFlag"));// 交易类型
		if (body.get("tranMinAmt")!=null) {
			inputMap.put("tranMinAmt", Double.parseDouble(body.get("tranMinAmt") + ""));// 交易最小金额
		}
		if (body.get("tranMaxAmt")!=null) {
			inputMap.put("tranMaxAmt", Double.parseDouble(body.get("tranMaxAmt") + ""));// 交易最大金额
		}

		Map<String, Object> resultMap = EsbConsumerUtil.clientRequest(inputMap);
		Map<String, Object> backMap = new HashMap();// 返回的map
		Map<String, Object> data = null;
		log.info("响应信息：[{}]", JSONObject.fromObject(resultMap));
		if (!CodeUtil.CODE_200.getCode().equals(resultMap.get("code"))) {
			return ResultUtil.resultMap(resultMap.get("msg").toString(), ResultCode.RESUTL_CODE_FAILURE.code(),
					resultMap.get("data"));
		} else {
			data = (Map<String, Object>) resultMap.get("data");
			List listArray = (List) data.get("listArray");
			List<Object> realList = new ArrayList<Object>();
			double expenditure = 0.0;// 支出
			double income = 0.0;// 收入
			int count = 0;
			for (int i = 0; i < listArray.size(); i++) {
				Map<String, Object> realDate = new HashMap<String, Object>();
				Map<String, Object> params = (Map<String, Object>) listArray.get(i);
				String drCrFlag = (String) params.get("drCrFlag");// 借贷标识1-借方(支出)2-贷方(收入)
				Double amt = (double) params.get("amt");
				if ("1".equals(drCrFlag) && amt != null) {// 支出
					expenditure = AmountUtil.add(expenditure, amt, 2);
					realDate.put("amt", "-" + amt);// 交易金额
					realDate.put("drCrFlagName", "支出");//
				} else if ("2".equals(drCrFlag) && amt != null) {
					income = AmountUtil.add(income, amt, 2);
					realDate.put("amt", "+" + amt);// 交易金额
					realDate.put("drCrFlagName", "收入");//
				}
				realDate.put("acctName", (String) params.get("acctName"));// 账户名称
				realDate.put("oppoAcctName", (String) params.get("oppoAcctName"));// 对方户名
				realDate.put("balance", params.get("balance") + "");// 余额
				realDate.put("tranRelAcctNo", (String) params.get("tranRelAcctNo"));// 对应账号
				realDate.put("oriTranTime", (String) params.get("oriTranTime"));// 原交易时间
				realDate.put("oriTranDate", DateUtils.getdatefor_yyyy_MM_dd((String) params.get("oriTranDate")));// 原交易日期
				realDate.put("postscript", (String) params.get("postscript"));// 附言（交易描述）
				realDate.put("oriTranCode", (String) params.get("oriTranCode"));// 交易码
				realDate.put("certNo", (String) params.get("certNo"));// 交易凭证
				realDate.put("drCrFlag", (String) params.get("drCrFlag"));//

				realDate.put("remark", (String) params.get("remark"));//
				realDate.put("cashTranFlag", (String) params.get("cashTranFlag"));// 现转标志
				realDate.put("tranRelAcctNo", (String) params.get("tranRelAcctNo"));// 对方账号卡号
				realDate.put("acctNo", (String) params.get("acctNo"));// ACCT_NO
				realDate.put("terminalType", (String) params.get("terminalType"));// 终端类型
				realDate.put("clientNo", (String) params.get("clientNo"));// 客户号
				realDate.put("tellSeqNo", params.get("tellSeqNo") + "");//
				realList.add(realDate);
				++count;
			}
			backMap.put("dateInterval", dateInterval);
			backMap.put("startDate", DateUtils.getdatefor_yyyy_MM_dd(startDate));
			backMap.put("endDate", DateUtils.getdatefor_yyyy_MM_dd(endDate));
			backMap.put("expenditure", expenditure + "");
			backMap.put("income", income + "");
			backMap.put("total", count + "");
			backMap.put("queryResultArray", realList);

		}

		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), backMap);
	}

	//////////////////////////// 以下是添加账户
	/**
	 * 添加下挂账户
	 */
	@Override
	public Map<String, Object> addHangingAccount(Map<String, String> paramMap) {

	
	/*	Map<String, Object> maps = commonService.verifyTransPassword(paramMap);
		if (!CodeUtil.CODE_200.getCode().equals(maps.get("code"))) {
			log.info("校验交易密码 响应结果:{}", maps.get("msg"));
			return maps;
		}*/
		Map<String, Object> m = new HashMap<String, Object>();
		for (Map.Entry<String, String> entry : paramMap.entrySet()) {
			m.put(entry.getKey(), entry.getValue());
		}

		return myCardPackageService.goAddCard(m);
	}

	/**
	 * 下挂账户验证 个人网银可加挂列表查询
	 */
	@SuppressWarnings("null")
	public List getHangingAccountList(Map<String, String> reqBody) {
		// 组装入参Map
		List resultList = new ArrayList();
		Map<String, Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> reqSysHead = RequestMap.getReqSysHead("11003000017", "05", "",
				BusinessConstant.SOURCRE_TYPE_PMBBS);
		Map<String, Object> userMap = RequestMap.getInputUserInfo((String) reqBody.get("clientSeqNo"));
		if (userMap.get("clientNo") == null) {
			return null;
		}
		Map<?, ?> acclist = RedisUtils.getUserInfo(reqBody.get("clientSeqNo"));
		if (acclist != null) {
			List list = (List) acclist.get("acctArray");
			List newList = new ArrayList();
			for (int i = 0; i < list.size(); i++) {
				Map m = (Map) list.get(i);
				Map newMap = new HashMap();
				newMap.put("acctNo", m.get("acctNo"));// 账号
				newMap.put("acctSerialNo", m.get("acctSerialNo"));// 账户序号
				newMap.put("acctAlias", m.get("acctAlias"));// 账户别名
				newMap.put("branchType", m.get("acctType"));
				newMap.put("openOrgan", m.get("openBranch"));
				newList.add(newMap);
			}
			inputMap.put("globalId", acclist.get("globalId"));// 证件号码
			inputMap.put("globalType", acclist.get("globalType"));// 证件类型
			inputMap.put("listArray", newList);

		}
		inputMap.put("bussName", "TransJnlsQry");
		inputMap.put("userNo", userMap.get("eUserNo"));
		inputMap.put("eUserNo", userMap.get("eUserNo"));
		reqSysHead.putAll(userMap);
		// 调用核心计价因子报文头入参
		inputMap.put("sysHead", reqSysHead);
		// inputMap.put("logonType", "R");//登录方式
		inputMap.put("sendBranchId", "");// 发送方机构标识码inputMap.put("listArray", list);
		inputMap.put("clientIp", reqBody.get("ip"));// 客户端ip
		inputMap.put("ip", "");// IP地址
		inputMap.put("userId", userMap.get("userId"));// IP地址
		Map<String, Object> data = null;
		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);

		log.info("响应【{}】", JSONObject.fromObject(result));
		if (!CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
			return null;
		}
		
		Map<String, Object> resultData = (Map<String, Object>) result.get("data");
		resultList = (List) resultData.get("queryResultArray");// 得到的可挂账户 的列表

		String acctNo = reqBody.get("acctNo");// 页面传过来的参数
		List backList = new ArrayList();// 过滤的卡号 得到子卡
		for (int i = 0; i < resultList.size(); i++) {
			Map<String, Object> map = (Map<String, Object>) resultList.get(i);
			String map_acctNo=(String) map.get("acctNo");
			
			if (map_acctNo.contains(acctNo)) {
				
				backList.add(map);
			}
		}
		return backList;
	}
//6224271190391118
//6224271190391118-105
	@Override
	public Map<String, Object> hangAndSolutionAccount(Map<String, String> paramMap) {
		log.info("入参:[{}]", JSONObject.fromObject(paramMap));
		Map<String, Object> inputMap = new HashMap<String, Object>();

		Map<String, Object> reqSysHead = RequestMap.getReqSysHead(TransCodeEnum.HANGANDSOLUTION.getServiceCode(),
				TransCodeEnum.HANGANDSOLUTION.getServiceSence(), "0110", SysConstans.ABP_CODE, "");

		Map<String, Object> userMap = RequestMap.getInputUserInfo((String) paramMap.get("clientSeqNo"));
		if (userMap.get("clientNo") == null) {
			return userMap;
		}
		reqSysHead.putAll(userMap);

		inputMap.put("lostAcctNo", paramMap.get("lostAcctNo"));// 挂失账号
		inputMap.put("lostType", paramMap.get("lostType"));// 挂失类型 0-挂失 2-解挂
		inputMap.put("acctType", paramMap.get("acctType"));
		// 调用核心计价因子入参
		inputMap.put("sysHead", reqSysHead);

		Map<String, Object> result = EsbConsumerUtil.clientRequest(inputMap);
		log.info("esb 响应[{}]", JSONObject.fromObject(result));
		if (CodeUtil.CODE_200.getCode().equals(result.get("code"))) {
			return ResultUtil.resultMap(result.get("msg").toString(), result.get("code").toString(), null);
		} else {
			return ResultUtil.resultMap(CodeUtil.CODE_10019.getName(), CodeUtil.CODE_10019.getCode(), null);
		}
	}

	/**
	 * 卡明细信息查询,活期账户金额汇总
	 */
	@Override
	public Map<String, Object> queryAcctDetial(Map<String, Object> paramsMap) {
		Map<String,Object> inputMap = new HashMap<String, Object>();
		Map<String, Object> reqSysHead =RequestMap.getReqSysHead(
				   TransCodeEnum.ACCOUNT_DETAIL.getServiceCode(),
				   TransCodeEnum.ACCOUNT_DETAIL.getServiceSence(), 
				   "", 
				   SysConstans.ABP_CODE, 
				   "");
        Map<String, Object> userMap = RequestMap.getInputUserInfo(paramsMap.get("clientSeqNo").toString());
        if (userMap.get("clientNo") == null) {
            return userMap;
        }
		String acctNo = (String)paramsMap.get("acctNo");
        reqSysHead.putAll(userMap);
		//调用核心计价因子报文头入参    
		inputMap.put("sysHead", reqSysHead);
		if(acctNo!=null) {
			String[] acctArr = acctNo.split("-");
			inputMap.put("acctNo", acctArr[0]);
			if(acctArr.length>1) {
				inputMap.put("acctSerialNo", acctArr[1]);
			}
			//查询明细
			Map<String, Object> resultMap  = acctDetailQry(inputMap);
			if (CodeUtil.CODE_200.getCode().equals(resultMap.get("code"))) {
				log.info("响应"+JSONObject.fromObject(resultMap));
				return resultMap;
			}else {
				return ResultUtil.resultMap(CodeUtil.CODE_500.getName(), CodeUtil.CODE_500.getCode(), resultMap.get("data"));
			}
		}else {
			//循环读取计算金额
			Map<String, Object> loginInfo =RedisUtils.getUserInfo((String)paramsMap.get("clientSeqNo"));
			JSONArray acctList= JSONArray.fromObject(loginInfo.get("acctArray"));
			double actBal = 0;
			for(int i=0 ; i<acctList.size(); i++) {
				JSONObject json = acctList.getJSONObject(i);
				if("PSAV".equals(json.getString("acctType"))) {//只查活期账户
					acctNo = json.getString("acctNo");
					String[] acctArr = acctNo.split("-");
					if(acctArr.length>1) {
						inputMap.put("acctNo", acctArr[0]);
						inputMap.put("acctSerialNo", acctArr[1]);
						Map<String, Object> resultMap  = acctDetailQry(inputMap);
						if (CodeUtil.CODE_200.getCode().equals(resultMap.get("code"))) {
							JSONObject actualBal = JSONObject.fromObject(resultMap.get("data"));
							actBal = actBal+(actualBal==null?0:Double.parseDouble(actualBal.getString("actualBal")));//可用余额
						}
					}
				}

			}
			Map<String,Object> resMap = new HashMap<>();
			resMap.put("actualBalTotal", actBal);
			
			return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), resMap);
			
		}		
	}  
	
	private  Map<String,Object> acctDetailQry(Map<String,Object> reqMap){
		
		Map<String, Object> resultMap = EsbConsumerUtil.clientRequest(reqMap);
		return resultMap;
	}

	
	
	@Override
	public Map<String, Object> queryExpendAndIn(Map<String, Object> paramsMap) {

		Map<String, Object> resultMap = new HashMap<String, Object>();

		Map<String, String> p = new HashMap<String, String>();
		for (Entry<String, Object> entry : paramsMap.entrySet()) {
			p.put(entry.getKey(), entry.getValue() + "");
		}

		Map<?, ?> acclist = RedisUtils.getUserInfo((String) p.get("clientSeqNo"));
		List newList = new ArrayList();
		if (acclist != null) {
			List list = (List) acclist.get("acctArray");

			for (int i = 0; i < list.size(); i++) {
				Map m = (Map) list.get(i);
				Map newMap = new HashMap();

				if ("PSAV".equals(m.get("acctType"))) {//活期账户
					String[] acc = m.get("acctNo").toString().split("-");
					paramsMap.put("dateInterval", "02");// 查询本月
					paramsMap.put("currentNum", "1");
					paramsMap.put("totalNum", Integer.MAX_VALUE + "");// 账户别名
					if (acc.length == 2) {
						paramsMap.put("acctNo", acc[0]);// 账号
						paramsMap.put("acctSerialNo", acc[1]);// 账户序号
					} else {
						paramsMap.put("acctNo", acc[0]);// 账号
						paramsMap.put("acctSerialNo", "");// 账户序号
					}
					newList.add(queryAccountTransactionDetail(paramsMap));
				}
			}

		}

		// 处理数据

		double expenditure = 0.0;// 支出
		double income = 0.0;// 收入
		String startDate = "";
		String endDate = "";

		for (int i = 0; i < newList.size(); i++) {

			Map<String, Object> map = (Map<String, Object>) newList.get(i);
			if (CodeUtil.CODE_200.getCode().equals(map.get("code"))) {
				Map dateMap = (Map) map.get("data");
				expenditure = AmountUtil.add(expenditure, Double.parseDouble((String) dateMap.get("expenditure")), 2);
				income = AmountUtil.add(income, Double.parseDouble((String) dateMap.get("income")), 2);
				startDate = dateMap.get("startDate") + "";
				endDate = dateMap.get("endDate") + "";
			}
		}

		resultMap.put("startDate", startDate);
		resultMap.put("endDate", endDate);
		resultMap.put("expenditure", expenditure+"");
		resultMap.put("income", income+"");

		return ResultUtil.resultMap(CodeUtil.CODE_200.getName(), CodeUtil.CODE_200.getCode(), resultMap);
	}
}
